//
//  CCFirstPageViewController.m
//  CwinCar
//
//  Created by dlios on 15/10/15.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "CCFirstPageViewController.h"
#import "CCFirtstPageCollectionViewCell.h"
@interface CCFirstPageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
//    CCFirtstPageCollectionViewCell *firstCell;
    NSInteger count;
}
@property (nonatomic, retain) NSMutableArray *buttonArray;
@property (nonatomic, retain) UIScrollView *buttonSCview;
@property (nonatomic, retain) UICollectionView *collectionView;
@end

@implementation CCFirstPageViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    count = 0;
    [self creatCollectionView];
 

    [self creatButtonSCview];
  
}
- (void)creatButtonSCview
{
    self.buttonSCview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, mainScree_Height / 66.7, mainScreen_Width, mainScree_Height / 22.233)];
    self.buttonSCview.delegate = self;
    [self.buttonSCview setPagingEnabled:NO];
    [self.buttonSCview showsHorizontalScrollIndicator];
    self.buttonSCview.showsHorizontalScrollIndicator = NO;
    [self.buttonSCview setContentSize:CGSizeMake(mainScreen_Width * 1.3, 0)];
    [self.navigationController.navigationBar addSubview:self.buttonSCview];
    
    self.buttonArray = [[NSMutableArray alloc]init];
    NSArray *array = @[@"最新",@"概念车",@"激情赛车",@"对比选车",@"新闻",@"新车谍照"];
    for (int i = 0; i < 6; i++)
    {
        UIButton *sixButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [sixButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [sixButton setTitle:[array objectAtIndex:i] forState:UIControlStateNormal];
       
        [sixButton setFrame:CGRectMake(count + i * 10, 0 , [CCStringWithWidth WidthWithText:array[i] height:mainScree_Height / 22.233 font:[UIFont systemFontOfSize:14.]]+20, mainScree_Height / 22.233)];
        count += [CCStringWithWidth WidthWithText:array[i] height:mainScree_Height / 22.233 font:[UIFont systemFontOfSize:14.]]+20;
        
//        sixButton.titleLabel.textAlignment = NSTextAlignmentCenter;
//        sixButton.layer.borderWidth = 0.5;
//        sixButton.layer.cornerRadius = 5;
        sixButton.layer.borderColor = [UIColor cyanColor].CGColor;
        [sixButton.titleLabel setFont:[UIFont systemFontOfSize:14.]];
        
        [sixButton addTarget:self action:@selector(sixClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArray addObject:sixButton];
        [self.buttonSCview addSubview:sixButton];
    }
;
}

- (void)sixClick:(UIButton *)sender
{
    
    for (int i = 0; i < 6; i++)
    {
        if (self.buttonArray[i] == sender)
        {
             [[self.buttonArray[i]titleLabel] setFont:[UIFont boldSystemFontOfSize:16.]];
            [self.collectionView setContentOffset:CGPointMake(mainScreen_Width * i, 0) animated:YES];
            if (i > 2)
            {
                [self.buttonSCview setContentOffset:CGPointMake(mainScreen_Width / 4.6875 * (i - 2)-100, 0)];
            }
        }
        else{
            [[self.buttonArray[i]titleLabel] setFont:[UIFont systemFontOfSize:14.]];
        }
    }
    
}


-(void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc]init];
    flowLayOut.itemSize = CGSizeMake(mainScreen_Width, mainScree_Height-44);
    flowLayOut.minimumInteritemSpacing = 0;
    flowLayOut.minimumLineSpacing = 0;
    flowLayOut.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayOut.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, mainScree_Height) collectionViewLayout:flowLayOut];

    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
    _collectionView.showsHorizontalScrollIndicator = YES;
    [_collectionView registerClass:[CCFirtstPageCollectionViewCell class] forCellWithReuseIdentifier:@"collectcell"];
    [self.view addSubview:_collectionView];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CCFirtstPageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectcell" forIndexPath:indexPath];
   
    cell.item = indexPath.item;

//    cell.dataSource = self;
//    cell.delegate = self;
    return cell;
    
    
}
-(void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    for (int i = 0; i < 6; i++)
    {
        if (self.collectionView.contentOffset.x == self.view.frame.size.width * i)
        {
            [self.buttonArray[i] setFont:[UIFont systemFontOfSize:17.]];
            
            if (i > 2) {
                [self.buttonSCview setContentOffset:CGPointMake(mainScreen_Width / 4.6875 * (i - 2)-100, 0)];
            }
            if (i <= 1) {
                [self.buttonSCview setContentOffset:CGPointMake(0, 0)];
            }
            
        }else
        {
            [self.buttonArray[i] setFont:[UIFont systemFontOfSize:14.]];
        }
    }

   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
